module Utils
  module Messages
    module View
      def message?
        flash[:notice] || flash[:warning] || flash[:error]
      end

      def show_messages
        messages = ""
        messages << format_message("notice") if flash[:notice] 
        messages << format_message("warning") if flash[:warning] 
        messages << format_message("error") if flash[:error] 
        messages
      end

      private
      def format_message(type)
        return <<-msg 
          <p id="#{type}"><strong>#{type.capitalize}:</strong><br/>#{flash[type.to_sym]}</p>
        msg
      end
    end #end view module

    module Manage
      def add_notice(key,value)
        add_message("notice",key,value)
      end

      def add_warning(key,value)
        add_message("warning",key,value)
      end

      def add_error(key,value)
        add_message("error",key,value)
      end

      def clear_all_messages
        clear_notices
        clear_warnings
        clear_errors
      end

      def clear_notices
        flash[:notice] = nil
      end

      def clear_warnings
        flash[:warning] = nil
      end

      def clear_errors
        flash[:error] = nil
      end

      private
      def add_message(type,key,value)
        flash[type.to_sym] = Hash.new unless flash[type.to_sym]
        flash[type.to_sym][key] = ":<br/>" + value + "<br/><br/>"
      end
    end#end manage module
  end
end
